<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="库位编号" prop="locationId">
        <el-input
          v-model="queryParams.locationId"
          placeholder="请输入库位编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="助记码" prop="pyName">
        <el-input
          v-model="queryParams.pyName"
          placeholder="请输入助记码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库编号" prop="storageId">
        <el-input
          v-model="queryParams.storageId"
          placeholder="请输入仓库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="islock" prop="islock">
        <el-input
          v-model="queryParams.islock"
          placeholder="请输入islock"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="轴承重量" prop="bearweight">
        <el-input
          v-model="queryParams.bearweight"
          placeholder="请输入轴承重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版本" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人" prop="operatorid">
        <el-input
          v-model="queryParams.operatorid"
          placeholder="请输入操作人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作时间" prop="operatedate">
        <el-date-picker clearable
          v-model="queryParams.operatedate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择操作时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="堆垛机编号" prop="decid">
        <el-input
          v-model="queryParams.decid"
          placeholder="请输入堆垛机编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="入库输送线" prop="inline">
        <el-input
          v-model="queryParams.inline"
          placeholder="请输入入库输送线"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库输送线" prop="outline">
        <el-input
          v-model="queryParams.outline"
          placeholder="请输入出库输送线"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
     
     
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:location:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:location:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:location:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:location:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border
    ref="filterTable" v-loading="loading" :data="locationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column sortable fixed label="主键" align="center" prop="id" />
      <el-table-column sortable label="库位编号" align="center" prop="locationId" />
      <el-table-column label="助记码" align="center" prop="pyName" />
      <el-table-column sortable label="仓库编号" align="center" prop="storageId" />
      <el-table-column label="islock" align="center" prop="islock" />
      <el-table-column sortable label="长" align="center" prop="locationLong" />
      <el-table-column sortable label="宽" align="center" prop="locationWidth" />
      <el-table-column sortable label="高" align="center" prop="locationHeight" />
      <el-table-column sortable label="体积" align="center" prop="volume" />
      <el-table-column label="hasvolume" align="center" prop="hasvolume" />
      <el-table-column sortable label="轴承重量" align="center" prop="bearweight" />
      <el-table-column label="abcId" align="center" prop="abcId" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="操作人" align="center" prop="operatorid" />
      <el-table-column sortable label="操作时间" align="center" prop="operatedate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operatedate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="堆垛机编号" align="center" prop="decid" />
      <el-table-column label="入库站台" align="center" prop="instand" />
      <el-table-column label="出库站台" align="center" prop="outstand" />
      <el-table-column label="入库输送线" align="center" prop="inline" />
      <el-table-column label="出库输送线" align="center" prop="outline" />
      <el-table-column label="行" align="center" prop="row" />
      <el-table-column label="列" align="center" prop="cln" />
      <el-table-column label="层级" align="center" prop="layer" />
      <el-table-column label="深度" align="center" prop="depth" />
      <el-table-column label="入库站台" align="center" prop="instand1" />
      <el-table-column label="入库站台" align="center" prop="instand2" />
      <el-table-column label="出库站台" align="center" prop="outstand1" />
      <el-table-column label="入库站台" align="center" prop="instand3" />
      <el-table-column label="出库站台" align="center" prop="outstand2" />
      <el-table-column label="出库站台" align="center" prop="outstand11" />
      <el-table-column label="" align="center" prop="deep" />
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:location:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:location:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row >
          <el-col :span="12">
            <el-form-item label="库位编号"  prop="locationId">
              <el-input v-model="form.locationId" placeholder="请输入库位编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="助记码" prop="pyName">
              <el-input v-model="form.pyName" type="textarea" placeholder="请输入助记码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="仓库编号"  prop="storageId">
              <el-input v-model="form.storageId" placeholder="请输入仓库编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="islock" prop="islock">
              <el-input v-model="form.islock" type="textarea" placeholder="请输入islock"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="长"  prop="locationLong">
              <el-input v-model="form.locationLong" placeholder="请输入长"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宽" prop="locationWidth">
              <el-input v-model="form.locationWidth" type="textarea" placeholder="请输入宽"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="高"  prop="locationHeight">
              <el-input v-model="form.locationHeight" placeholder="请输入高"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="体积" prop="volume">
              <el-input v-model="form.volume" type="textarea" placeholder="请输入体积"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="hasvolume"  prop="hasvolume">
              <el-input v-model="form.hasvolume" placeholder="请输入hasvolume"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="轴承重量" prop="bearweight">
              <el-input v-model="form.bearweight" type="textarea" placeholder="请输入轴承重量"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="abcId"  prop="abcId">
              <el-input v-model="form.abcId" placeholder="请输入abcId"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" type="textarea" placeholder="请输入排序"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="备注"  prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本" prop="version">
              <el-input v-model="form.version" type="textarea" placeholder="请输入版本"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="操作人"  prop="operatorid">
              <el-input v-model="form.operatorid" placeholder="请输入操作人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间" prop="operatedate">
              <el-date-picker clearable
            v-model="form.operatedate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择操作时间">
          </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="堆垛机编号" prop="decid">
          <el-input v-model="form.decid" placeholder="请输入堆垛机编号" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库站台" prop="instand">
          <el-input v-model="form.instand" placeholder="请输入入库站台" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="入库输送线" prop="inline">
          <el-input v-model="form.inline" placeholder="请输入入库输送线" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出库输送线" prop="outline">
          <el-input v-model="form.outline" placeholder="请输入出库输送线" />
        </el-form-item>
          </el-col>
        </el-row> 
        <el-row >
          <el-col :span="12">
            <el-form-item label="行" prop="row">
          <el-input v-model="form.row" placeholder="请输入行" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="列" prop="cln">
          <el-input v-model="form.cln" placeholder="请输入列" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="层级" prop="layer">
          <el-input v-model="form.layer" placeholder="请输入层级" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="深度" prop="depth">
          <el-input v-model="form.depth" placeholder="请输入深度" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="入库站台" prop="instand1">
          <el-input v-model="form.instand1" placeholder="请输入入库站台" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库站台" prop="instand2">
          <el-input v-model="form.instand2" placeholder="请输入入库站台" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="出库站台" prop="outstand1">
          <el-input v-model="form.outstand1" placeholder="请输入出库站台" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库站台" prop="instand3">
          <el-input v-model="form.instand3" placeholder="请输入入库站台" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="出库站台" prop="outstand2">
          <el-input v-model="form.outstand2" placeholder="请输入出库站台" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出库站台" prop="outstand11">
          <el-input v-model="form.outstand11" placeholder="请输入出库站台" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="deep" prop="deep">
          <el-input v-model="form.deep" placeholder="请输入deep" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出库站台" prop="outstand11">
          <el-input v-model="form.outstand11" placeholder="请输入出库站台" />
        </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLocation, getLocation, delLocation, addLocation, updateLocation } from "@/api/system/location";

export default {
  name: "Location",
  dicts: ['status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库位表格数据
      locationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        locationId: null,
        pyName: null,
        storageId: null,
        islock: null,
        locationLong: null,
        locationWidth: null,
        locationHeight: null,
        volume: null,
        hasvolume: null,
        bearweight: null,
        abcId: null,
        sort: null,
        status: null,
        version: null,
        operatorid: null,
        operatedate: null,
        decid: null,
        instand: null,
        outstand: null,
        inline: null,
        outline: null,
        row: null,
        cln: null,
        layer: null,
        depth: null,
        instand1: null,
        instand2: null,
        outstand1: null,
        instand3: null,
        outstand2: null,
        outstand11: null,
        deep: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        locationId: [
          { required: true, message: "库位编号不能为空", trigger: "blur" }
        ],
        storageId: [
          { required: true, message: "仓库编号不能为空", trigger: "blur" }
        ],
        locationLong: [
          { required: false,validator: this.validateNumber, trigger: "blur" }
        ],
        locationWidth: [
          { required: false,validator: this.validateNumber, trigger: "blur" }
        ],
        locationHeight: [
          { required: false,validator: this.validateNumber, trigger: "blur" }
        ],
        volume: [
          { required: false,validator: this.validateNumber, trigger: "blur" }
        ],
        hasvolume: [
          { required: false,validator: this.validateNumber, trigger: "blur" }
        ],
        bearweight: [
          { required: false,validator: this.validateNumber, trigger: "blur" }
        ],
        sort: [
          { required: false,validator: this.validateNumber, trigger: "blur" }
        ],
        operatorid: [
          { required: true, message: "操作人不能为空", trigger: "blur" }
        ],
        operatedate: [
          { required: true, message: "操作时间不能为空", trigger: "blur" }
        ],
        decid: [
          { required: true, message: "堆垛机编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    validateNumber(rule, value, callback) {  
      const regex = /^\d+(\.\d+)?$/; // 正则表达式，匹配整数或小数  
  
  // 检查是否为空，如果允许空值则通过验证  
  if (value == null || value === '') {  
    // 这里没有错误，因为允许空值  
    callback(); // 调用callback无参数表示验证通过  
    return; // 退出函数  
  }  
  
  // 如果值不为空，则检查是否符合正则表达式  
  if (!regex.test(value)) {  
    // 如果值不符合正则表达式，返回错误  
    return callback(new Error('请输入有效的数字（可以包含小数）'));  
  }  
  
  // 如果验证通过，调用callback  
  callback();  
  },
    /** 查询库位列表 */
    getList() {
      this.loading = true;
      listLocation(this.queryParams).then(response => {
        this.locationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        locationId: null,
        pyName: null,
        storageId: null,
        islock: null,
        locationLong: null,
        locationWidth: null,
        locationHeight: null,
        volume: null,
        hasvolume: null,
        bearweight: null,
        abcId: null,
        sort: null,
        status: null,
        remark: null,
        version: null,
        operatorid: null,
        operatedate: null,
        decid: null,
        instand: null,
        outstand: null,
        inline: null,
        outline: null,
        row: null,
        cln: null,
        layer: null,
        depth: null,
        instand1: null,
        instand2: null,
        outstand1: null,
        instand3: null,
        outstand2: null,
        outstand11: null,
        deep: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLocation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLocation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLocation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库位编号为"' + ids + '"的数据项？').then(function() {
        return delLocation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/location/export', {
        ...this.queryParams
      }, `location_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
